// if someone wants to rewrite this they should
// i did a p. hacky job =p

/*
 (Only read this file if you're interested in the implementation
  of the chat filtering system, or documentation of procs).

  The MessageLocker class answers the question of whether a message
  should be sent.

  (I recommend hooking the muting system to this.
  There is a stub proc called IsMuted(client/client) which
  can be overriden to read from the muted list. This negates the
  necessity to check whether a client is muted separately in
  The Saloon's say verb.)
*/

MessageLocker
	var
		// The minimum length of a message.
		message_minimum_length = 1

		// The last few messages by a client, up to messages_per_interval.
		list/last_messages = new

		// An _interval (in 10th/s of seconds)
		message_interval = 50

		// Current interval.
		current_interval

		// The number of messages allowed per that interval.
		messages_per_interval = 5

		// The number of messages allowed to repeat themselves.
		messages_repeat_max = 3


	/* Constructor for MessageLocker.

		[Arg]				[Effect]
		interval			An _interval (in 10th/s of seconds)
		per_interval		The number of messages allowed per that interval.
		repeat_max			The number of messages allowed to repeat themselves.
		minimum_length		Minimum message length.
	*/
	New (interval = 30, per_interval = 5, repeat_max = 3, minimum_length = 1)
		setMessageInterval(interval)
		setMessagesPerInterval(per_interval)
		setMessagesRepeatMax(repeat_max)
		setMinimumMessageLength(minimum_length)

	proc
		/* Gets/sets the message interval. */
		setMessageInterval(num)			message_interval = num
		getMessageInterval(num)			return message_interval

		/* Gets/sets the number of messages allowed per that interval. */
		setMessagesPerInterval(num)		messages_per_interval = num
		getMessagesPerInterval(num)		return messages_per_interval

		/* Gets/sets the amount of messages allowed to repeat. */
		setMessagesRepeatMax(num)		messages_repeat_max = num
		getMessagesRepeatMax(num)		return messages_repeat_max

		/* Gets/sets minimum message length. */
		setMinimumMessageLength(num)	message_minimum_length = num
		getMinimumMessageLength()		return message_minimum_length


		/* Returns true if a client can send this message. */
		CanSend(client/client, message)
			return (length(message) >= getMinimumMessageLength() && !isLocked(client))
         
      /* updates message list if necessary. */
		UpdateMessages()         
			var/day_interval = world.time
			if (day_interval > current_interval)
				current_interval = day_interval + message_interval
				last_messages = new /list
            
		/* Returns true if a client's messages are locked (that is, they should not be sent). */
		isLocked(client/client)
			UpdateMessages() // gets current state of messages
			return	(!isAllowedMessagesPerInterval(client) 	\
					|| !isAllowedMessageRepeatMax(client) || 	\
					isMuted(client))

		/* Returns true if a client is allowed more messages this interval. */
		isAllowedMessagesPerInterval(client/client)
			return (!(client in last_messages) || (length(last_messages[client]) < getMessagesPerInterval()))

		/* Returns true if a client is allowed to repeat messages. */
		isAllowedMessageRepeatMax(client/client)

			if (!(client in last_messages))
				return TRUE

			var/count			= 0
			var/prev_message	= null

			for (var/message in last_messages[client])
				if (message == prev_message)
					count ++
					if (count + 1 >= messages_repeat_max) return FALSE
				else
					prev_message = message; count = 0

			return TRUE

		/* Returns true if a client is muted.
			(Stub, to be overriden) */
		isMuted(client/client)
			return FALSE

		/* Registers a message said by a client. */
		RegisterMessage(client/client, message)
			if (!(client in last_messages))
				last_messages[client] = list(message)
			else if (!length(last_messages[client]) <= getMessagesPerInterval())
				last_messages[client] += message
			else
				last_messages[client] = list(message)